package com.asset.dispoal.defaultCompanyRole.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.defaultCompanyRole.domain.db.entity.GbDefaultCompanyRole;
import com.asset.dispoal.defaultCompanyRole.domain.db.mapper.GbDefaultCompanyRoleMapper;
import com.asset.dispoal.defaultCompanyRole.dto.GbDefaultCompanyRoleRequest;
import com.asset.dispoal.defaultCompanyRole.dto.GbDefaultCompanyRoleResponse;
import com.asset.dispoal.defaultCompanyRole.service.converter.GbDefaultCompanyRoleConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/17$
 */
@Component
public class GbDefaultCompanyRoleQueryPage implements BaseCommand<GbDefaultCompanyRoleRequest, Result<PageVO<GbDefaultCompanyRoleResponse>>> {


    @Autowired
    GbDefaultCompanyRoleMapper gbDefaultCompanyRoleMapper;

    @Override
    public Result<PageVO<GbDefaultCompanyRoleResponse>> execute(GbDefaultCompanyRoleRequest request) {
        GbDefaultCompanyRole gbDefaultCompanyRole = GbDefaultCompanyRoleConverter.INSTANCE.reqToDo(request);
        gbDefaultCompanyRole.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int count = gbDefaultCompanyRoleMapper.count(gbDefaultCompanyRole);
        final Page<GbDefaultCompanyRole> page = PagingQuery.getPage(count, gbDefaultCompanyRole);
        PageVO<GbDefaultCompanyRoleResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<GbDefaultCompanyRole> companyRoleList = gbDefaultCompanyRoleMapper.page(page);
            final List<GbDefaultCompanyRoleResponse> gbDefaultCompanyRoleResponses = GbDefaultCompanyRoleConverter.INSTANCE.doToRes(companyRoleList);
            pageVO.setResult(gbDefaultCompanyRoleResponses);
        }
        return Result.ok(pageVO);
    }
}
